package com.my.test.ctrl.file;

import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping("/ajaxfile")
public class AjaxFileUpLoad {

	@RequestMapping("index")
	public String index(){
		return "ajaxfileupload/index";
	}
//	
	
	@RequestMapping("upload")
    @ResponseBody
    public Map upload(HttpServletRequest request,  @RequestParam("uFile") CommonsMultipartFile uFile){
		Map<String,Object> resMap = new HashMap<String,Object>();
        File tmpDir = FileUtils.getTempDirectory();

        if (uFile != null) {
            //获取保存的路径，
            if (uFile.isEmpty()) {
                // 未选择文件
                resMap.put("respCode", "9000");
            } else{
                // 文件原名称
                String originFileName = uFile.getOriginalFilename();
//                String suffix = FileType.getSuffixByFilename(originFileName);
//                String newFileName= MrandomUtil.getUUID()+suffix;
                String path = tmpDir.getPath()+File.separator+originFileName;
                try {
                    //这里使用Apache的FileUtils方法来进行保存
                    FileUtils.copyInputStreamToFile(uFile.getInputStream(),
                    new File(tmpDir.getPath()+File.separator, originFileName));
                    resMap.put("respCode","0000");
                    resMap.put("path",path);
                } catch (IOException e) {
                    resMap.put("respCode", "9000");
                    e.printStackTrace();
                }
            }

        }
        return resMap;
    }
	
//	@RequestMapping("upload")
//    @ResponseBody
//    public String upload(HttpServletRequest request,  ModelMap map){
//        try {
//            MultipartHttpServletRequest mRequest = (MultipartHttpServletRequest) request;
//            Map<String, MultipartFile> fileMap = mRequest.getFileMap();
//            Iterator<Map.Entry<String, MultipartFile>> iterator = fileMap.entrySet().iterator();
//            while(iterator.hasNext()) {
//                Map.Entry<String, MultipartFile> next = iterator.next();
//                MultipartFile multipartFile = next.getValue();
//                
//                File localFile = new File(request.getContextPath()+"12345"+multipartFile.getOriginalFilename());
//                multipartFile.transferTo(localFile);
//                return localFile.getPath();
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//        return null;
//    }
}
